package com.yxinsur.imc.web.controller.business;

import com.yxinsur.imc.business.enums.RateTypeEnum;
import com.yxinsur.imc.business.excel.RateTemplate;
import com.yxinsur.imc.business.form.InsRateForm;
import com.yxinsur.imc.business.form.InsSearchForm;
import com.yxinsur.imc.business.form.OrderRiskForm;
import com.yxinsur.imc.business.service.IImcInsRateService;
import com.yxinsur.imc.business.service.IImcInsService;
import com.yxinsur.imc.business.vo.InsRateVo;
import com.yxinsur.imc.business.vo.InsVo;
import com.yxinsur.imc.business.vo.RepoInsExportVo;
import com.yxinsur.imc.business.vo.RepoInsVo;
import com.yxinsur.imc.common.core.controller.BaseController;
import com.yxinsur.imc.common.core.domain.AjaxResult;
import com.yxinsur.imc.common.core.page.TableDataInfo;
import com.yxinsur.imc.common.exception.BusinessException;
import com.yxinsur.imc.common.utils.StringUtils;
import com.yxinsur.imc.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 险种产品费率表Controller
 *
 * @author imc
 * @date 2021-04-29
 */
@Api(value="产品费率管理",tags={"4.4.产品费率管理接口服务"})
@RestController
@RequestMapping("/business/rate")
public class ImcInsRateController extends BaseController
{
    @Autowired
    private IImcInsRateService imcInsRateService;
    @Autowired
    private IImcInsService imcInsService;

    /**
     * 查询险种产品列表
     */
    @ApiOperation("费率配置的产品列表")
    @GetMapping("/inslist")
    public TableDataInfo list(InsSearchForm insForm)
    {
        startPage();
        List<InsVo> list = imcInsService.selectImcInsList(insForm);
        return getDataTable(list);
    }


    /**
     * 导出产品费率配置信息
     */
    @ApiOperation("产品费率配置信息")
    @GetMapping("/export/insList")
    public AjaxResult exportInsList(InsSearchForm insForm)
    {
        List<RepoInsExportVo> list = imcInsService.exportInsList(insForm);
        ExcelUtil<RepoInsExportVo> util = new ExcelUtil<>(RepoInsExportVo.class);
        return util.exportExcel(list, "产品费率配置信息");
    }

    /**
     * 根据repoInsId导出产品费率信息
     */
    @ApiOperation("根据repoInsId导出产品费率信息")
    @GetMapping("/export/insList/{repoInsId}")
    public AjaxResult exportInsListByRepoInsId(@PathVariable Long repoInsId)
    {
        List<RepoInsExportVo> list = imcInsService.exportInsListByRepoInsId(repoInsId);
        ExcelUtil<RepoInsExportVo> util = new ExcelUtil<>(RepoInsExportVo.class);
        return util.exportExcel(list, "产品费率详情信息");
    }

    /**
     * 查询险种费率列表
     */
    @ApiOperation("指定产品的费率详情列表")
    @GetMapping("/list/{repoInsId}")
    public TableDataInfo insRateList(@PathVariable Long repoInsId)
    {
        startPage();
        List<InsRateVo> list = imcInsRateService.selectImcInsRateList(repoInsId);
        return getDataTable(list);
    }

    @ApiOperation("险种费率模板下载")
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<RateTemplate> util = new ExcelUtil<RateTemplate>(RateTemplate.class);
        return util.importTemplateExcel("险种费率数据");
    }
    @ApiOperation("险种佣金费率数据导入（orgId：机构ID,repoSupplierId:供应商ID）")
    @PostMapping("/import/comm/{orgId}/{repoSupplierId}")
    public AjaxResult importCommData(MultipartFile file,@PathVariable("orgId") Long orgId, @PathVariable("repoSupplierId") Long repoSupplierId) throws Exception
    {
        ExcelUtil<RateTemplate> util = new ExcelUtil<RateTemplate>(RateTemplate.class);
        List<RateTemplate> rateList = util.importExcel(file.getInputStream());
        String message = this.imcInsRateService.importInsRate(RateTypeEnum.C.getCode(),orgId,repoSupplierId,rateList);
        return AjaxResult.success(message);
    }
    @ApiOperation("险种手续费率数据导入（orgId：机构ID,repoSupplierId:供应商ID）")
    @PostMapping("/import/fee/{orgId}/{repoSupplierId}")
    public AjaxResult importFeeData(MultipartFile file,@PathVariable("orgId") Long orgId, @PathVariable("repoSupplierId") Long repoSupplierId) throws Exception
    {
        ExcelUtil<RateTemplate> util = new ExcelUtil<RateTemplate>(RateTemplate.class);
        List<RateTemplate> rateList = util.importExcel(file.getInputStream());
        String message = this.imcInsRateService.importInsRate(RateTypeEnum.F.getCode(),orgId,repoSupplierId,rateList);
        return AjaxResult.success(message);
    }
    @ApiOperation("险种折标费率数据导入（orgId：机构ID,repoSupplierId:供应商ID）")
    @PostMapping("/import/stand/{orgId}/{repoSupplierId}")
    public AjaxResult importData(MultipartFile file,@PathVariable("orgId") Long orgId, @PathVariable("repoSupplierId") Long repoSupplierId) throws Exception
    {
        ExcelUtil<RateTemplate> util = new ExcelUtil<RateTemplate>(RateTemplate.class);
        List<RateTemplate> rateList = util.importExcel(file.getInputStream());
        String message = this.imcInsRateService.importInsRate(RateTypeEnum.R.getCode(),orgId,repoSupplierId,rateList);
        return AjaxResult.success(message);
    }
    @ApiOperation("供应商折标系数数据导入（orgId：机构ID,repoSupplierId:供应商ID）")
    @PostMapping("/import/supplierRate/{orgId}/{repoSupplierId}")
    public AjaxResult importSupplierRateData(MultipartFile file,@PathVariable("orgId") Long orgId, @PathVariable("repoSupplierId") Long repoSupplierId) throws Exception
    {
        ExcelUtil<RateTemplate> util = new ExcelUtil<RateTemplate>(RateTemplate.class);
        List<RateTemplate> rateList = util.importExcel(file.getInputStream());
        String message = this.imcInsRateService.importInsRate(RateTypeEnum.SR.getCode(),orgId,repoSupplierId,rateList);
        return AjaxResult.success(message);
    }
    @ApiOperation("年期折标系数数据导入（orgId：机构ID,repoSupplierId:供应商ID）")
    @PostMapping("/import/yearScale/{orgId}/{repoSupplierId}")
    public AjaxResult importYearScaleData(MultipartFile file,@PathVariable("orgId") Long orgId, @PathVariable("repoSupplierId") Long repoSupplierId) throws Exception
    {
        ExcelUtil<RateTemplate> util = new ExcelUtil<RateTemplate>(RateTemplate.class);
        List<RateTemplate> rateList = util.importExcel(file.getInputStream());
        String message = this.imcInsRateService.importInsRate(RateTypeEnum.YS.getCode(),orgId,repoSupplierId,rateList);
        return AjaxResult.success(message);
    }
    /**
     * 新增险种产品费率表
     */
    @ApiOperation("新增单条费率（折标/佣金/手续费率）")
    @PostMapping
    public AjaxResult add(@RequestBody InsRateForm insRateForm)
    {
        return toAjax(imcInsRateService.insertImcInsRate(insRateForm));
    }
    /**
     * 修改险种产品费率表
     */
    @ApiOperation("更新单条费率（折标/佣金/手续费率）")
    @PutMapping
    public AjaxResult edit(@RequestBody InsRateForm insRateForm)
    {
        return toAjax(imcInsRateService.updateImcInsRate(insRateForm));
    }

    /**
     * 删除险种产品费率表
     */
    @ApiOperation("删除单条或多条费率（折标/佣金/手续费率）")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(imcInsRateService.deleteImcInsRateByIds(ids));
    }

    /**
     * 获取产品四个费率
     */
    @ApiOperation("获取产品四个费率")
    @PostMapping("/match/rates")
    public AjaxResult matchRate(@RequestBody OrderRiskForm riskForm)
    {
        return AjaxResult.success(this.imcInsRateService.getRates(riskForm));
    }

    /**
     * 获取产品费率列表
     */
    @ApiOperation("获取产品费率表")
    @PostMapping("/match/list")
    public AjaxResult rateList(@RequestBody OrderRiskForm riskForm)
    {
        return AjaxResult.success(this.imcInsRateService.getRateList(riskForm));
    }

}
